package io.renren.wh.merchant.controller;

import io.renren.common.utils.Constant;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.wh.channel.entity.ChannelInfoEntity;
import io.renren.wh.channel.service.ChannelInfoService;
import io.renren.wh.interfaces.entity.InterfacesEntity;
import io.renren.wh.interfaces.service.InterfacesService;
import io.renren.wh.merchant.entity.RateChannelEntity;
import io.renren.wh.merchant.entity.RateEntity;
import io.renren.wh.merchant.service.RateService;
import io.renren.wh.merchant.vo.ChannelInfoShowVo;
import io.renren.wh.merchant.vo.RateChannelVo;
import io.renren.wh.merchant.vo.RateUpdateVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;


/**
 *
 *
 * @author liulinqiu
 * @email liulinqiu1997@163.com
 * @date 2020-07-23 09:22:16
 */
@Api(value = "商户费率信息接口",tags = "商户费率信息接口")
@RestController
@RequestMapping("wh/rate")
public class RateController {
    @Autowired
    private RateService rateService;
    @Autowired
    private ChannelInfoService channelInfoService;
    @Autowired
    private InterfacesService interfacesService;

    /**
     * 列表
     */
    @ApiOperation(value = "获取商户费率信息列表", notes = "获取商户费率信息列表", httpMethod = "GET")
    @RequestMapping("/list")
    @RequiresPermissions("wh:rate:list")
    public R list(@ApiParam(name = "page", value = "第几页", required = false)
                      @RequestParam(required = false) String page,
                  @ApiParam(name = "limit", value = "每页显示的条数", required = false)
                      @RequestParam(required = false) String limit,
                  @ApiParam(name = "merchantName", value = "商户名称", required = false)
                      @RequestParam(required = false)String merchantName,
                  @ApiParam(name = "interfaceId", value = "接口ID", required = false)
                      @RequestParam(required = false)String interfaceId){
        Map<String,Object> params = new HashMap<>();
        params.put("merchantname",merchantName);
        params.put("interfaceId",interfaceId);
        params.put(Constant.PAGE,page);
        params.put(Constant.LIMIT,limit);

        PageUtils pages = rateService.queryPage(params);
        return R.ok().put("page", pages);
    }


    /**
     * 信息
     */
    @ApiOperation(value = "获取商户费率信息", notes = "获取商户费率信息", httpMethod = "GET")
    @RequestMapping("/info/{id}")
    @RequiresPermissions("wh:rate:info")
    public R info(@ApiParam(name = "id", value = "商户费率信息ID",required = true)
            @PathVariable("id") Integer id){
		RateEntity rate = rateService.getById(id);

		//获取接口标识
        Integer interfaceCode = rate.getInterfacecode();
        //查询接口信息
        InterfacesEntity interfacesEntity = interfacesService.selectByPayType(interfaceCode);
        String interfaceName = interfacesEntity.getInterfacename();
        return R.ok().put("rate", rate).put("interfaceName",interfaceName);
    }

    /**
     * 信息
     */
    @ApiOperation(value = "获取接口的所有通道信息", notes = "获取接口的所有通道信息", httpMethod = "GET")
    @RequestMapping("/getChannelInfo")
    @RequiresPermissions("wh:rateinteface:list")
    public R getChannel(@ApiParam(name = "interfaceId", value = "接口ID",required = true)
                        @RequestParam("interfaceId") String interfaceId,
                        @ApiParam(name = "rateId", value = "商户费率ID",required = true)
                        @RequestParam("rateId") Integer rateId){
        Map<String, Object> params=new HashMap<>();

        params.put("interfaceId",interfaceId);
        params.put(Constant.PAGE,"0");
        params.put(Constant.LIMIT,"100");

        PageUtils page = channelInfoService.queryPage(params);
        //查询当前接口的所有通道
        List<ChannelInfoEntity> list=(List<ChannelInfoEntity>)page.getList();
        //查询当前费率已选通道
        List<Integer> seletedChannl=rateService.queryChannelByRateId(rateId);

        List<ChannelInfoShowVo> vlist=new ArrayList<>();
        for (ChannelInfoEntity channel:list) {
            if(channel.getStatus()==0) {//状态正常的通道
                ChannelInfoShowVo vo = new ChannelInfoShowVo();
                vo.setChannelId(channel.getId());
                vo.setChannelname(channel.getChannelname());
                vo.setChannelrate(channel.getChannelrate());
                vo.setInterfacename(channel.getInterfacename());
                Boolean isSelected=seletedChannl.contains(channel.getId());
                vo.setIsSelected(isSelected);
                vlist.add(vo);
            }
        }
        return R.ok().put("channelList",vlist).put("rateId",rateId);
    }

    /**
     * 保存
     */
    @ApiOperation(value = "新增商户费率信息", notes = "新增商户费率信息", httpMethod = "POST")
    @RequestMapping("/save")
    @RequiresPermissions("wh:rate:save")
    public R save(@RequestBody RateEntity rate){
		rateService.save(rate);

        return R.ok();
    }

    /**
     * 修改
     */
    @ApiOperation(value = "修改商户费率信息", notes = "修改商户费率信息", httpMethod = "POST")
    @RequestMapping("/update")
    @RequiresPermissions("wh:rate:update")
    public R update(@RequestBody RateUpdateVo vo){
        RateEntity rate=new RateEntity();
        rate.setId(vo.getId());
        rate.setRate(vo.getRate());

		rateService.updateById(rate);
        return R.ok();
    }

    /**
     * 删除
     */
    @ApiOperation(value = "删除商户费率信息", notes = "删除商户费率信息", httpMethod = "DELETE")
    @RequestMapping("/delete")
    @RequiresPermissions("wh:rate:delete")
    public R delete(@ApiParam(name = "ids", value = "商户费率信息ID") @RequestBody Integer[] ids){
		rateService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 修改
     */
    @ApiOperation(value = "保存商户费率通道信息", notes = "保存商户费率通道信息", httpMethod = "POST")
    @RequestMapping("/saveRateChannel")
    @RequiresPermissions("wh:ratechannel:save")
    public R saveRateChannel(@RequestBody List<RateChannelVo> volist){
        List<RateChannelEntity> list=new ArrayList<>();
        for (RateChannelVo vo:volist) {
            RateChannelEntity rateChannelEntity=new RateChannelEntity();
            rateChannelEntity.setMerchantRateId(vo.getMerchantRateId());
            rateChannelEntity.setChannelId(vo.getChannelId());
            list.add(rateChannelEntity);
        }
        rateService.insertRateChannel(list);
        return R.ok();
    }

}
